package com.example.controller;


import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.Ticket;
import com.example.service.TicketService;
import com.example.vo.OrderVO;
import com.example.vo.PayStatusVO;
import com.example.vo.TicketVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "售票管理模块")
@Slf4j
@RestController
@RequestMapping("/api/tickets")
public class TicketController {

    @Autowired
    private TicketService ticketService;

    @ApiOperation("购票")
    @PostMapping
    public Result save(@RequestBody @Validated TicketVO ticketVO) {
        log.info("新增购票记录,ticketVO:{}", ticketVO);
        return ticketService.save(ticketVO);
    }

    @ApiOperation("退票")
    @DeleteMapping("/{ticketID}")
    public Result delete(@PathVariable Integer ticketID) {
        // 给一个需要退票的id，则会将该票下的所有相同订单号的都进行退票操作
        log.info("要删除的票,ticketID{}", ticketID);
        return ticketService.delete(ticketID);
    }

    @ApiOperation("查询购票记录")
    @GetMapping
    public Result list(@ApiParam("要查询第几页，默认是第一页") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam("每页要查询几条数据，默认是10") @RequestParam(defaultValue = "10") Integer pageSize,
                       @ApiParam("状态") String status,
                       @ApiParam("用户ID，非必须") @RequestParam(required = false) Integer userID,
                       @ApiParam("赛事ID，非必须") Integer eventID,
                       @ApiParam("座位ID 1-300，非必须") Integer ticketID) {
        PageBean ticketList = ticketService.list(userID, eventID, ticketID,status, page, pageSize);
        return Result.success(ticketList);
    }

    @ApiOperation("支付宝支付回调通知")
    @PostMapping("/alipay/notify")
    public String alipayNotify(HttpServletRequest request) {
        return ticketService.parseNotify(request);
    }

    @ApiOperation("支付完成订单状态更新")
    @PostMapping("/pay/ok")
    public Result payStatusOk(@RequestBody PayStatusVO payStatusVO) {
        return ticketService.payStatusOk(payStatusVO);
    }
}
